import java.util.Scanner;

public class Main1_9 {
    public static int isTrue(int[][] arr) {
        //宫位
        int[] arr2 = new int[100];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                //一个宫
                for (int k = i * 3; k < i * 3 + 3; k++) {
                    for (int l = j * 3; l < j * 3 + 3; l++) {
                        arr2[arr[k][l] - 1] += 1;
                    }
                }
                for (int k = 0; k < arr2.length; k++) {
                    if(arr2[k] >= 2) {
                        return 0;
                    }
                    arr2[k] = 0;
                }
            }
        }
        for (int i = 0; i < 9; i++) {
            //一行
            for (int j = 0; j < 9; j++) {
                arr2[arr[i][j] - 1] += 1;
            }
            for (int j = 0; j < arr2.length; j++) {
                if(arr2[j] >= 2) {
                    return 0;
                }
                arr2[j] = 0;
            }
        }
        for (int i = 0; i < 9; i++) {
            //一列
            for (int j = 0; j < 9; j++) {
                arr2[arr[j][i] - 1] += 1;
            }
            for (int j = 0; j < arr2.length; j++) {
                if(arr2[j] >= 2) {
                    return 0;
                }
                arr2[j] = 0;
            }
        }
        return 1;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][][] arr = new int[n][9][9];
        int[] ret = new int[n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 9; j++) {
                for (int k = 0; k < 9; k++) {
                    arr[i][j][k] = in.nextInt();
                    if(arr[i][j][k] > 9 || arr[i][j][k] < 1) {
                        ret[i] = 1;
                    }
                }
            }
        }
        for (int i = 0; i < n; i++) {
            if(ret[i] != 1) {
                System.out.println(isTrue(arr[i]));
            } else {
                System.out.println(0);
            }
        }
    }
}
